/**
 * 
 */
package kernel_gui.gui;

import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import kernel_gui.gui.extended.ELoginFrame;
import kernel_gui.interfaces.IGUI;
import main.Kernel;

/**
 * @author Yoyo
 *
 */
public class GUIControl implements IGUI{
	/**
	 * @param args
	 */
	
	private static IGUI currentFrame = null;
	private	static GUIControl instance = null;
	private static Kernel kernel = null;
	private static boolean init = false;
	
	
	/**
	 * Empty constructor for singleton
	 */
	private GUIControl(){};
	
	
	/**
	 * Initialize the GUIControl with kernel instance.
	 * NOTE: you must initialize the GUIControl before asking for a GUIControlInstance,
	 * 		Trying to initialize the GUIControl more than once will cause an IllegalStateException
	 * @param kernel
	 */
	public static void initGUIControl(Kernel kernel) throws IllegalStateException{
		
		if(!init){
			GUIControl.kernel = kernel;
			init = true;
		}else
			throw new IllegalStateException("The GUIControl can only be initialized once in a process life");
	}
	
	/**
	 * Get a singleton instance of GUIControl.
	 * throws a IllegalStateException in case the GUIControl wasn't initialized
	 * 
	 * @return a GUI controller which implements the IGUI interface
	 */
	public static IGUI getGUIControlInstance() throws IllegalStateException{
		if(!init)
			throw new IllegalStateException("One must initialize the GUIControl before asking for an instance of it");
		
		if(GUIControl.instance == null)
			GUIControl.instance = new GUIControl();
		
		return instance;
	}
	
	/**
	 * When a frame switches to another frame it must set that frame as the current visible frame 
	 * @param currentFrame
	 */
	public static void setCurrentVisibleFrame(IGUI currentFrame){
		if(!init)
			throw new IllegalStateException("One must initialize the GUIControl before asking for an instance of it");
		
		GUIControl.currentFrame = currentFrame;
	}
	
	public static Kernel getKernel(){
		if(!init)
			throw new IllegalStateException("One must initialize the GUIControl before asking for an instance of it");
		
		return GUIControl.kernel;
	}
	
	
	public static void startGUI(){
		
		/*Try setting GUI's feel&look to the current OS theme*/
		
    		try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (ClassNotFoundException e) {
				/*Nothing to do here, using the default feel&look*/
			} catch (InstantiationException e) {
				/*Nothing to do here, using the default feel&look*/
			} catch (IllegalAccessException e) {
				/*Nothing to do here, using the default feel&look*/
			} catch (UnsupportedLookAndFeelException e) {
				/*Nothing to do here, using the default feel&look*/
			}
		
		
		
		new ELoginFrame().setVisible(true);
		
	}

	/* (non-Javadoc)
	 * @see interfaces.IGUI#returnAnswer(int, java.lang.Object)
	 */
	@Override
	public void returnAnswer(int requestID, Object answer) { 
		GUIControl.currentFrame.returnAnswer(requestID, answer);		
	}

	/* (non-Javadoc)
	 * @see interfaces.IGUI#returnError(int, java.lang.String, boolean)
	 */
	@Override
	public void returnError(int requestID, String errorMSG, boolean fatalError) {
		GUIControl.currentFrame.returnError(requestID, errorMSG, fatalError);
	}

}
